<template>
    <el-dialog v-if="dialogVisible" :close-on-click-modal='false' title="创建推广位" :visible.sync="dialogVisible" @close="resetForm('addPddForm')" width="400px">
        <el-form label-position="top" :model="addPddForm" :hide-required-asterisk='true' ref="addPddForm" :rules="addPddRules" size="medium">
            <el-form-item label="用户ID" prop="user_id">
                <el-select v-model="addPddForm.user_id" placeholder="请选择" filterable style="width:100%">
                    <el-option v-for="item in userList" :key="item.id" :label="item.username" :value="item.id">
                    </el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="推广位名称" prop="name">
                <el-input v-model="addPddForm.name" placeholder="推广位名称"></el-input>
            </el-form-item>
            <!-- <el-form-item label="商品ID" prop="goods_id">
                <el-input v-model="addPddForm.goods_id" placeholder="商品ID"></el-input>
            </el-form-item> -->
        </el-form>
        <span slot="footer" class="dialog-footer">
            <el-button size="small" @click="resetForm('addPddForm')" class="el-button-width">取 消</el-button>
            <el-button size="small" type="primary" @click="submitForm('addPddForm')" :disabled="loading" :loading="loading" class="el-button-width">立即创建</el-button>
        </span>
    </el-dialog>
</template>

<script>
export default {
    name: "",
    data() {
        return {
            loading: false,
            dialogVisible: false,
            userList:[],
            addPddForm: {
                name: "",
                user_id: "",
                // goods_id:''
            },
            addPddRules: {
                name: [
                    {
                        required: true,
                        message: "请输入推广位名称",
                        trigger: ["blur", "change"],
                    },
                ],
                user_id: [
                    {
                        required: true,
                        message: "请选择用户ID",
                        trigger: ["blur", "change"],
                    },
                ],
                goods_id: [
                    {
                        required: true,
                        message: "请输入商品ID",
                        trigger: ["blur", "change"],
                    },
                ],
            },
        };
    },
    created() {},
    methods: {
        // 提交添加
        submitForm(formName) {
            this.$refs[formName].validate(async (valid) => {
                if (valid) {
                    this.loading = true;
                    const { data: res } = await this.$http.post(
                        "cid/Pdd/pddGenerate",
                        this.addPddForm
                    );
                    if (res.code !== 200) {
                        this.$message.error(res.msg);
                        this.loading = false;
                        return;
                    } else {
                        this.$message({
                            message: "添加成功",
                            type: "success",
                        });
                        this.loading = false;
                        this.$emit("getDataList");
                        this.resetForm(formName);
                    }
                } else {
                    this.loading = false;
                    return false;
                }
            });
        },

        //获取用户列表
        async getUserList() {
            const { data: res } = await this.$http.post("cid/Pdd/pddUserList");
            if (res.code !== 200) {
                this.$message.error(res.msg);
                return;
            } else {
                this.userList = res.data;
            }
        },

        // 关闭重置表单
        resetForm(formName) {
            this.$refs[formName].resetFields();
            this.dialogVisible = false;
        },
    },
    components: {},
};
</script>


<style scoped>
.el-form-item {
    margin-bottom: 16px;
}
</style>
